<template>
	<view style="text-align: center;">
		<image style="width: 200rpx;height: 200rpx;border-radius: 50%;margin: 0 auto;margin-top: 100rpx;" src="../../static/logo.jpg" mode=""></image>
		<view class="btn" @click="" style="margin-top: 50rpx;">
			一键快捷登录/注册
		</view>
		<view class="btn-ok login" style="margin-top: 50rpx;"><button style="width: 200rpx;;height: 90rpx;"  type="success" :hair-line='false' :custom-style="customStyleOk"
		open-type="getPhoneNumber" @getphonenumber="authPhone"> 登录</button></view>
		<!-- <view class="btn-ok login" style="margin-top: 50rpx;">
			<button style="width: 200rpx;;height: 90rpx;" @click="handleAuth"> 登录</button>
			 <button bindtap="handleAuth">授权登录</button>
			
			
		</view> -->
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone:'',
				name:'',
				image:'',
				id:'',
				is_stor:'',
				storId:'',
				balance:'',
			}
		},
		onLoad() {
			
		},
		methods: {
			authPhone(e){
				this.$req('GET',this.$api.getPhone,{
					code: e.detail.code
				}).then(res => {
					console.log(res,"ff")
					this.phone = res.data.phoneNumber
					uni.setStorageSync('phone',this.phone)
					this.getLogin()
				})
			},
			getLogin() {
				let that = this
				uni.login({
					success(res) {
						console.log(res,'打印res')
						that.$req('GET', that.$api.getopenid, {
							code: res.code
						}).then((resp) => {
							console.log(resp,'打印resp');
							that.openId = resp.data.openid
							that.register()
						})
					}
				})
			},
			
			register(){
				this.$req('POST',this.$api.register,{
					phone:this.phone,
					openid: this.openId,
				}).then(res => {
					uni.$u.toast("登录成功")
					this.id = res.data.id
					uni.setStorageSync('userId',this.id)
					this.getUserInfo()
					this.whatLogin = uni.getStorageSync('userId')
					
					setTimeout(() => {
						uni.navigateBack()
					},1000)
					
					// this.hh()
				})
			},
			getUserInfo(){
				this.$req('GET',this.$api.getUser,{
					id:uni.getStorageSync('userId')
				}).then(ress =>{
					this.name = ress.data.name
					this.image = ress.data.avatar
					
					this.is_stor = ress.data.is_store
					this.storId = ress.data.store.id
					this.balance = ress.data.store.balance
					
					uni.setStorageSync('name',this.name)
					uni.setStorageSync('image',this.image)
					uni.setStorageSync('is_stor',this.is_stor)
					uni.setStorageSync('storId',this.storId)
					uni.setStorageSync('balance',this.balance)
					
				})
			},
		}
	}
</script>

<style>
	page{
		background-color: aliceblue;
	}



</style>